package com.artisan.provider.controller;

import com.artisan.provider.contanent.Contanents;
import com.artisan.provider.covtransformation.CovOrderTransformation;
import com.artisan.provider.domain.ZgOrder;
import com.artisan.provider.dto.ZgOrderDto;
import com.artisan.provider.service.ZgOrderService;
import com.artisan.provider.utils.Request;
import com.artisan.provider.vo.ZgOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ZgOrderController {

    @Autowired
    ZgOrderService zgOrderService;



    /***
     * 查询所有优惠券信息
     * @param zgOrderDto
     * @param page
     * @param limit
     * @return
     */
    @PostMapping(value = "/api/order/find/all/{page}/{limit}")
    public Request zgOrderFindAll(@RequestBody(required = false) ZgOrderDto zgOrderDto,
                                        @PathVariable(value = "page",required = false) int page,
                                        @PathVariable(value = "limit",required = false)int limit
                                        ){

        List<ZgOrder> zgOrders = zgOrderService.findAll(zgOrderDto,page,limit);
        List<ZgOrderVo> zgOrderVos = CovOrderTransformation.INSTANCE.zgOrderVos(zgOrders);
        if (zgOrders != null){
            return new Request(Contanents.DATA_SUCCESS,true,zgOrderVos);
        }else{
            return new Request(Contanents.DATA_FAILED,false,new ArrayList<>());
        }

    }

    /**
     * 数据的添加业务
     * @param zgOrderDto
     * @return
     */
    @PostMapping(value = "/api/order/add")
    public Request zgOrderAdd(@RequestBody ZgOrderDto zgOrderDto){
        ZgOrder zgOrder = CovOrderTransformation.INSTANCE.zgOrder(zgOrderDto);
        if(zgOrderService.zgOrderAdd(zgOrder) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的修改业务
     * @param zgOrderDto
     * @return
     */
    @PutMapping(value = "/api/order/update")
    public Request zgOrderUpdate(@RequestBody ZgOrderDto zgOrderDto){
        ZgOrder zgOrder = CovOrderTransformation.INSTANCE.zgOrder(zgOrderDto);
        if(zgOrderService.zgOrderUpdate(zgOrder) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的删除业务
     * @param id
     * @return
     */
    @DeleteMapping(value = "/api/order/delete/{id}")
    public Request zgOrderDelete(@PathVariable(value = "id")int id){

        if(zgOrderService.zgOrderDelete(id) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

}
